
//
//  **** COMPLETE ****
//
//  Defines_Includes.c
//  ECGVer2
//
//  Created by FRANCIS PAPINEAU on 12-01-01.
//  Copyright 2012 carleton university. All rights reserved.
//
//  Serial = Xbee/ Comms
//  Serail1 = LCD
//  Serial2 = VDIP

/*________________________________________________________________________________*/

//defines/includes
#include <EEPROM.h>
#include <PString.h>
#include "max6675.h"

/*________________________________________________________________________________*/
/*__________________________________ Serial ______________________________________*/
/*________________________________________________________________________________*/

//#define DEBUG
#define MEGA

#define Serial_BAUD_Rate        115200
#define Serial1_BAUD_Rate       115200
#define FAST_Serial2_BAUD_Rate  115200
#define SLOW_Serial2_BAUD_Rate  9600

/*________________________________________________________________________________*/
/*____________________________________ Pins ______________________________________*/
/*________________________________________________________________________________*/
//Digital Pins

// Digital Pin 2 is attached to the powerfail interrupt

#define RedLED                  3
#define IrLED                   4
#define PulseOx                 5
#define StartupLED              6
#define ECGPulse                7

// For some reason these pins are defined as int_8 on the MAX6675 lib...
// maybe change this later.

/*#define*/ int GND       =     8;
/*#define*/ int VCC       =     9;
/*#define*/ int DS        =     10;
/*#define*/ int CS        =     11;
/*#define*/ int CLK       =     12;
#define CheckSensors            13
#define SetupLED                14

//Analog Pins
#define ECG                     0
#define Light                   1
#define Resp                    2
#define AmbTemp                 3
#define ECGBatt                 4
#define Battery                 5
#define SysPwr                  6

#define OKbutton                21


#define buttonsUp               0 // start with the buttons in the 'not pressed' state

byte buttonState = buttonsUp;

#define pad                     9850  // balance/pad resistor value, set this to
// the measured resistance of your pad resistor
#definevcc                      4.91  // only used for display purposes
#define thermr                  10000 // thermistor nominal resistance
#define KEY_WAIT                1000  // Wait for potential other key press
#define VersionID               1    // Firmware version ID
#define SessionID               1    // the SessionID

// guardian variables
boolean USBConnected       =    false;
boolean USBStore           =    false;
boolean XBeeConnected      =    false;
boolean USB                =    false;
boolean SensorsOK          =    false;
boolean menusChanged       =    false;

// string constants
const String SessionFolder =    "\\Session";
const String TempHeader    =    "Time, Tympanic, Ambient, Diff.\n";
const String VitalsHeader  =    "Time, ECG, ECG Pulse, RED, IR, ERROR, RESP\n";
const String OthersHeader  =    "Time, Light, Battery, ECG Battery\n";

/*________________________________________________________________________________*/

char Connection, Store;
int LoggingState     =           0;
int DEBUGsendRandom  =           0;
int ON               =           0;
int BuffersFull      =           0;
int pulse            =           0;
int StartTime        =           0;
int menus            =           0;
double temp          =           0;
double tempIr        =           0;
double tempError     =           0;
double tempRed       =           0;
double tempECG       =           0;
double tempRESP      =           0;
double tempLight     =           0;
double AmbientTemp   =           0;
double tempBat       =           0;
double tempECGBat    =           0;
double ECGPulseTemp  =           0;
String ACK           = 
"********************************************************\n"
"The System has booted up and no issue has undergone.\n" 
"The System has initialized the following:\n"
"\t->initLCD();\n"
"\t->initEEPROM();\n"
"\t->initbuttons();\n" 
"\t->initLEDPins();\n" 
"\t->initDigitalPins();\n" 
"\t->initSensors();\n" 
"\t->attachInterrupts();\n" 
"\t->initXbee();\n" 
"\t->initUSB();\n" 
"\t->initVDIP();\n" 
"\t->initBuffers();\n" 
"\t->initMenus();\n\n"
"This is an acknoledgement of smooth running.\n\n" 
"********************************************************\n" 
"Copyright. 2012 Frank and Thai's Med. R&D CANADA.\n" 
"********************************************************\n\n\n";

// in this one:
// time, ambientTemp, TympTemp, DiffTemp (7 characters -> in 140 we get 20 samples/page)

char vdipBuffer1[140];
PString logEntryTemp(vdipBuffer1, sizeof(vdipBuffer1)); // Create a PString object called logEntry

// in this one:
// time, ECG, ECG Pulse, SPO2 RAW, SPO2 IR, SPO2 ERROR, RESP (13 characters -> in 260 we get 20 samples/page)

char vdipBuffer2[260];
PString logEntryVitals(vdipBuffer2, sizeof(vdipBuffer2)); // Create a PString object called logEntry

// in this one:
// time, Light, Battery, ECG Battery (5 characters -> in 100 we get 20 samples/page)

char vdipBuffer3[140];
PString logEntryOthers(vdipBuffer3, sizeof(vdipBuffer3)); // Create a PString object called logEntry

/*________________________________________________________________________________*/
/*________________________________________________________________________________*/
/*________________________________________________________________________________*/

//
//  ***** COMPLETE *****
//
//  SpecialChar.c
//  ECGVer2
//
//  Created by FRANCIS PAPINEAU on 12-01-01.
//  Copyright 2012 carleton university. All rights reserved.
//



/*________________________________________________________________________________*/

// Defining Spceial Characters

// Empty Battery
const char EmptyBatt[] = {0x0e, 0x1b, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1f};

// Battery at 15%
const char Batt10[] = {0x0e, 0x1b, 0x11, 0x11, 0x11, 0x11, 0x1f, 0x1f};

// Battery at 30%
const char Batt30[] = {0x0e, 0x1b, 0x11, 0x11, 0x11, 0x1f, 0x1f, 0x1f};

// Battery at 45%
const char Batt45[] = {0x0e, 0x1b, 0x11, 0x11, 0x1f, 0x1f, 0x1f, 0x1f};

// Battery at 60%
const char Batt60[] = {0x0e, 0x1b, 0x11, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f};

// Battery at 75%
const char Batt75[] = {0x0e, 0x1b, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f};

// Battery at >90%
const char FullBatt[] = {0x0e, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f};

//Connected to PC Symbol:
const char Connected_PC_Symbol[] = {0x04, 0x0e, 0x15, 0x04, 0x04, 0x15, 0x0e, 0x04};

//Connected to XBee Symbol
const char Connected_XBee_Symbol[] = {0x00, 0x0e, 0x11, 0x15, 0x0e, 0x1f, 0x04, 0x04};

//USB Storage Symbol
const char USB_Store_Symbol[] = {0x00, 0x0e, 0x0a, 0x1f, 0x15, 0x1f, 0x15, 0x1f};

//DEBUG Char
const char DebugSymbol[] = {'D'};

//Sprite Character

const byte Sprite[] =
{0x42,0x4D,0x40,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
    0x00,0x00,0x02,0x04,0x00,0x00,0xC6,0x75,0x02,0x00,0xC6,0x75,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xE0,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x01,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xE0,0x01,0xFC,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x01,0xFC,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xE0,0x01,0xFC,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

//Diagram
//
//  ===||=== 
//     ||=|  
//     ||    

/*________________________________________________________________________________*/
/*________________________________________________________________________________*/
/*________________________________________________________________________________*/

//
//  **** COMPLETE ****
//
//  ECGVer2.c
//  ECGVer2
//
//  Created by FRANCIS PAPINEAU on 12-01-01.
//  Copyright 2012 carleton university. All rights reserved.
//

/* --------------------------------------------------------- */

void setup(){   
    // A visual Queue that the System is in
    // Setup Mode.
    // Pin 14 -> LED ON
    
    pinMode(SetupLED, OUTPUT);
    digitalWrite(SetupLED, HIGH);
    
    // ----- PRELIMINARY PROCEDURES ------
    
    //    -> init serial Connection to LCD
    
    initLCD();
    
    goToLine(1);
    LCDprint("***** Starting Up *****");
    delay(100);
    
    goToLine(2);
    LCDprint("  [OK]    ");
    
    
    // ----- SPLASH SCREEN PROCEDURES ------
    
    //    -> splash Screen
    //      -> ask for connections 
    //      -> Store USB?
    
    toggleSplashScreen();
    delay(1000);
    
    #ifdef MEGA
    
    // ----- EEPROM CHECK STARTUP PROCEDURES ------
    
    // init() the EEPROM Memory
    initEEPROM();
    
    // ----- PINS // INT // ISR PROCEDURES ------  
    
    //    -> init buttons
    //    -> init LED pins
    //    -> init Digital pins
    //    -> init Sensors
    
    //    -> attach interrupts
    
    initbutton();
    initLEDPins();
    initDigitalPins();
    initSensors();
    
    attachInterrupts();
    
    // ----- COMMS STARTUP PROCEDURES ------
    
    //    -> init Keyboard  
    //    -> init serial Connection to XBee
    //    -> init serial Connection to PC
    
    initXBee();
    initVDIP();
    
    // ----- MEM. & MENUS STARTUP PROCEDURES ------
    
    //      -> +1 to session ID
    //      -> Verify Version ID
    //    -> init Menu System
    //    -> Startup
    
    // Print the system summary and the Acknoledgement Signal
    // to show the status of the system before starting
    printACK_Sys_files();
    initMenus();
    #endif
    
    // ----- DEBUG STARTUP PROCEDURES ------
    #ifdef DEBUG
        XBeeConnected = false;
        USBConnected = false;
        USBStore = false;
        
        toggleDEBUGSplashScreen();
        delay(1000);
        
        clearLCD();
        
        goToLine(2);
        LCDprint("***** DEBUG MODE *****");
        
        printDebugSymbol();
    #endif
    #ifdef MEGA
    if (USBStore){
        openNewFiles();
        printHeaders();
    }
    #endif
    
    // A visual Queue that the System is now past
    // the Setup Mode.
    // Pin 14 -> LED OFF
    
    digitalWrite(SetupLED, LOW);
}

/* --------------------------------------------------------- */

void loop(void){      
    
    // A Visual Queue that the System is in a Rolling State
    // Pin 13 -> Lights up a Status LED
    
    digitalWrite(StartupLED, HIGH);
    
    if(menusChanged){
        gotoMenu(menu);
    }
    
    #ifdef DEBUG
        startDebugSequence();
        if(DEBUGsendRandom == 1){
            sendPseudoRandom();
        }
    #endif
    #ifdef MEGA
    goToLine(2);
    LCDprint("Running...");
    
    goToLine(3);
    LCDprint("Comms are ok...");
    
    
    // Process any commands from the host Terminal ONLY
    // IF THE BOARD IS CONNETED TO A CPU
    processTerminalCommands();
    
    if(USBConnected){
        // Echo data from the VDIP back to the host if conected to a PC
        processVdipBuffer();
    }
    
    if(XBeeConnected){
        sendSensorValues();
        printXbeeSymbol();
    }
    
    if(USBConnected){
        sendSensorValues();
        printPCSymbol();
    }
    
    if(USBStore){
        printUSBSTORE();
    }
    
    if(USBStore){
        FillUSBBuffers();
        
        if(BuffersFull == 1){
            printUSBBUffer();  
        }   
    }
    
    printBattLevel();
    #endif
}

/* --------------------------------------------------------- */

void startDebugSequence(void){
    
    initDebugDialog();
    startDebugSend();
    Connection = 'D';
    
}

/* --------------------------------------------------------- */

void attachInterrupts(void){
    clearLCD();
    
    // ----- ISR #1 STARTUP PROCEDURES ------
    
    // Installing Power Supply ISR
    // NEEDED TO DETERMINE IF A POWER
    // FAILURE HAS OCCURED
    
    goToLine(1);
    // Interrupt triggered by falling voltage on power supply input
    // Pin 20
    LCDprint(" * Attaching powerfail ISR      ");
    attachInterrupt(3, powerFail, FALLING);
    
    goToLine(2);
    LCDprint("   [OK]    ");
    
    // ----- ISR #2 STARTUP PROCEDURES ------
    
    // Installing the button ISR
    // Needed to go through menus
    
    goToLine(1);
    LCDprint(" * Attaching logging ISR        ");
    // Interrupt changes menus
    // Pin 21
    attachInterrupt(2, ButtonStateChange, FALLING);
        
    goToLine(2);
    LCDprint("    [OK]    ");
        
    goToLine(3);
    LCDprint("Start. Complete!");    
}
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */



